define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vip/user/index' + location.search,
                    add_url: 'vip/user/add',
                    edit_url: 'vip/user/edit',
                    del_url: 'vip/user/del',
                    multi_url: 'vip/user/multi',
                    table: 'vip_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('Username')},
                        {field: 'level', title: __('Level')},
                        {
                            field: 'money', title: __('Money'), operate:'BETWEEN',
                            formatter: function(value,row){
                                value = value.toString();
                                var point_part = "";//记录小数点后面的部分（包含小数点）
                                var num_pattern = /^(?!0)\d+(\.\d+)$/;//判断是否为数字
                                var split_num_pattern = /(?=\d|[,，])/;
                                var split_comma_pattern = /\d{3}(?!$)|\d+/g;
                                if (!num_pattern.test(value))
                                    return "";
                                else {
                                    point_part = num_pattern.exec(value)[1];
                                    if (point_part != null && point_part.length > 0)
                                        value = value.replace(point_part, "");
                                    var value_array = value.split(split_num_pattern);
                                    value_array = value_array.reverse();
                                    value = value_array.join("");
                                    var temp_array = value.match(split_comma_pattern);
                                    value = temp_array.join(",");
                                    value_array = value.split(split_num_pattern);
                                    value_array = value_array.reverse();
                                    value = value_array.join("");
                                    return value + point_part;
                                }
                            }
                        },
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            //当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //这里我们手动设置底部的值
                $("#money_box").text(data.extend.money);
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        payout: function () {
            // 修改状态
            $("#send_jl").click(function(){
                layer.confirm('是否修改状态？', {
                    btn: ['确认','取消'] //按钮
                }, function(index){
                    layer.close(index);
                    $.ajax({
                        url:"vip/user/send_status",
                        dateType:"json",
                        success:function(re)
                        {
                            if(re.status == 200){
                                Toastr.success(re.msg);
                            } else {
                                Toastr.error(re.msg);
                            }
                        }
                    })
                }, function(){
                    layer.msg('取消成功', {icon: 2});
                });
            });

            // down_haoyun
            $("#down_haoyun").click(function(){
                var down_time = $("[name='down_time']").val();
                var url = "/admin_66365.php/vip/excels/down_haoyun?down_time="+down_time;
                console.log(url);
                window.open(url);
            });
            // down_jinji
            $("#down_jinji").click(function(){
                var down_time = $("[name='down_time']").val();
                var url = "/admin_66365.php/vip/excels/down_jinji?down_time="+down_time;
                console.log(url);
                window.open(url);
            });
            // down_chouma
            $("#down_chouma").click(function(){
                var down_time = $("[name='down_time']").val();
                var url = "/admin_66365.php/vip/excels/down_chouma?down_time="+down_time;
                console.log(url);
                window.open(url);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});